$(function(){
	/*value：表示当前单元格中的值
	row：表示当前行
	index：表示当前行的下标*/
	function operateFormatter(value, row, index) {
		var btns = [
		    	'<button class="btn btn-danger" disabled="disabled">编辑</button>  ',
				'<button class="btn btn-danger" disabled="disabled">删除</button>  '
		    ]
		if(row.appUser == null && row.wxMiniUser == null && row.wxUser == null){
			btns.push('<button class="btn btn-danger un-bundling" disabled="disabled" data-toggle="modal" data-target="#bundlingModal">解绑</button>')
		}else{
			btns.push('<button class="btn btn-danger un-bundling" data-toggle="modal" data-target="#bundlingModal">解绑</button>')
		}
	    return btns.join('');
	}
	
	var device_id = 0;
	//表格操作
	window.operateEvents = {
		// 解绑
		'click .un-bundling': function (e, value, row, index) {
			device_id = row.id;
			if(row.appUser != null){
				$(".check-app").css('display', 'block');
				$(".check-app input").val(row.appUser.id);
				$(".check-app input").prop("checked", true);
			}
			if(row.wxMiniUser != null){
				$(".check-wmu").css('display', 'block');
				$(".check-wmu input").val(row.wxMiniUser.id);
				$(".check-wmu input").prop("checked", true);
			}
			if(row.wxUser != null){
				$(".check-wu").css('display', 'block');
				$(".check-wu input").val(row.wxUser.id);
				$(".check-wu input").prop("checked", true);
			}
		}
	}
	
	//模态框关闭事件
	$('#bundlingModal').on('hide.bs.modal', function () {
		$(".check-box").css('display', 'none');
		$(".check-box input").val(0);
		$(".check-box input").prop('checked', false);
	})　
	
	$("#bundlingBtn").click(function(){
		var appUserId = 0;
		var wxMiniUserId = 0;
		var wxUserId = 0;
		if($(".check-app input").prop('checked')){
			appUserId = $(".check-app input").val()
		}
		if($(".check-wmu input").prop('checked')){
			wxMiniUserId = $(".check-wmu input").val()
		}
		if($(".check-wu input").prop('checked')){
			wxUserId = $(".check-wu input").val()
		}
		
		$.ajax({
			type: "post",
			url: "/devices/unBundling",
			data: {
				id: device_id,
				appUserId: appUserId,
				wxMiniUserId: wxMiniUserId,
				wxUserId: wxUserId
			},
			success: function(result){
				if(result.code == 0){
					window.location.reload()//刷新当前页面
				}else{
					alert("解绑失败，请检查");
				}
			}
		})
	})
	
	//当值为0或null时显示无
	function CustomFormatter(value, row, index){
		if(value == 0 || value == null){
			return '无';
		}else{
			return value;
		}
	}
	
	//获取内容商
	var selectData = [];
	
	$.ajax({url:"/user/query",async: false,success:function(result){
		var _data = $.map(result.data.users, function (obj) {
			obj.text = obj.text || obj.account; 
			return obj;
		});
		selectData = _data
	}});
	
	//开启select2
	$('.js-data-example-ajax').select2({
		data: selectData
	});
	
	
	//bootstrap-table配置
	$('#devices-tab').bootstrapTable({
	    url: '/devices/fingPage',
	    toolbar: "#device-toolbar",
	    toolbarAlign:'right',
	    sidePagination: "server",
	    striped: true, // 是否显示行间隔色
	    pageNumber : 1, 
	    pageSize: 8,
	    pageList: [8, 25],
	    pagination: true, // 是否分页
	    sortable: true, // 是否启用排序
	    queryParams: function(params) {//上传服务器的参数
	        var temp = {
	        	pageNum : this.pageNumber,  
	        	pageSize : this.pageSize,
	        	deviceId : $("#deviceId").val(),
	        	mac: $("#deviceMAC").val(),
	        	account: $("#select2").select2("data")[0].account
	        };
	        return temp;
	    },
	    columns: [{
	            field: 'did',
	            title: '设备id',
	            formatter: CustomFormatter
	        },
	        {
	            field: 'mac',
	            title: 'mac地址',
	            formatter: CustomFormatter
	        },
	        {
	            field: 'user.account',
	            title: '内容商账号',
	            formatter: CustomFormatter
	        },
	        {
	            field: 'wxMiniUser.openid',
	            title: '绑定小程序用户',
	            formatter: CustomFormatter
	        },
	        {
	            field: 'appUser.username',
	            title: '绑定App用户',
	            formatter: CustomFormatter
	        },
	        {
	            field: 'wxUser.openid',
	            title: '绑定公众号用户',
	            formatter: CustomFormatter
	        },
	        {
	            field: '',
	            title: '操作',
	            align: 'center',
	            valign: 'middle',
	            formatter: operateFormatter,
	            events: window.operateEvents
	        },
	
	    ]
	});
	
	//查询
	$(document).on('click', "#searchBtn",function(){
	     $('#devices-tab').bootstrapTable('selectPage', 1, 'refresh');
	     $('#searchModal').modal('hide');
	});
})